package com.coocaa.ops.admin.v1.tab.convert;

import com.coocaa.ops.common.core.biz.tab.entity.TabPanelConfigInfo;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/13 15:47
 */
@UtilityClass
public class TabPanelRlsQueryConverter {

    public static List<TabPanelConfigInfo>  convertFinalPanelList(  List<TabPanelConfigInfo> commonPanelList,  List<TabPanelConfigInfo> dmpPanelList){
        TreeMap<Integer, TabPanelConfigInfo> tabPanelConfigInfoTreeMap  = InstanceUtil.newTreeMap();
        for (TabPanelConfigInfo tabPanelConfigInfo : commonPanelList){
            tabPanelConfigInfo.setIsDmpPanel(0);
            tabPanelConfigInfoTreeMap.put(tabPanelConfigInfo.getPanelSeq(), tabPanelConfigInfo);
        }
        if(CollectionUtils.isNotEmpty(dmpPanelList)){
            handleFilterDmp(tabPanelConfigInfoTreeMap, dmpPanelList);
        }
        return convert2List(tabPanelConfigInfoTreeMap);

    }

    private static  List<TabPanelConfigInfo>  convert2List(TreeMap<Integer, TabPanelConfigInfo> tabPanelConfigInfoTreeMap ){
        List<TabPanelConfigInfo> tabPanelConfigInfoList = InstanceUtil.newArrayList();
        for (Map.Entry<Integer, TabPanelConfigInfo> entry : tabPanelConfigInfoTreeMap.entrySet()){
            tabPanelConfigInfoList.add(entry.getValue());
        }
        return tabPanelConfigInfoList;
    }

    public static void handleFilterDmp( TreeMap<Integer, TabPanelConfigInfo> tabPanelConfigInfoTreeMap , List<TabPanelConfigInfo> dmpPanelList){
        for (TabPanelConfigInfo tabPanelConfigInfo : dmpPanelList){
            Integer panelSeq = tabPanelConfigInfo.getPanelSeq();
            if (tabPanelConfigInfoTreeMap.containsKey(panelSeq)){
                TabPanelConfigInfo exist = tabPanelConfigInfoTreeMap.get(panelSeq);
                List<TabPanelConfigInfo>  toSetDmpPanelList = exist.getDmpPanelList();
                if(null == toSetDmpPanelList){
                   toSetDmpPanelList =  InstanceUtil.newArrayList();
                }
                toSetDmpPanelList.add(tabPanelConfigInfo);
                exist.setDmpPanelList(toSetDmpPanelList);

            }else {
                TabPanelConfigInfo  newTabPanel  = new TabPanelConfigInfo();
                newTabPanel.setIsDmpPanel(1);
                List<TabPanelConfigInfo>  toSetDmpPanelList = InstanceUtil.newArrayList();
                toSetDmpPanelList.add(tabPanelConfigInfo);
                newTabPanel.setDmpPanelList(toSetDmpPanelList);
                tabPanelConfigInfoTreeMap.put(panelSeq, newTabPanel);
            }
        }
    }
}
